﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game.UIScripts
{
    class LevelComplete : Panel
    {
        private readonly UIController _ui;
        private readonly GameScreen _gameScreen;

        private readonly ContentManager _contentManager;
        private GraphicsDevice _graphicsDevice;
        private MouseState _previousMouseState;

        private Texture2D _pausebg;
        
        private readonly List<UIButton> _buttons = new List<UIButton>();

        private SpriteFont _fontTimer;
        public LevelComplete(IServiceProvider serviceProvider, GraphicsDevice graphicsDevice, ContentManager content, UIController controller, GameScreen game)
        {
            _contentManager = content;
            _graphicsDevice = graphicsDevice;
            _ui = controller;
            _gameScreen = game;
            LoadContent();
        }

        public void Reset()
        {

        }

        private void LoadContent()
        {
            _pausebg = _contentManager.Load<Texture2D>("Sprites/UI/LevelComplete/WINHUD");
            _fontTimer = _contentManager.Load<SpriteFont>("Sprites/UI/LevelComplete/score");
            _buttons.Add(new UIButton("CONTINUE", new Vector2(549.5f, 350), _contentManager.Load<Texture2D>("Sprites/UI/LevelComplete/CONTINUE"), true));
            _buttons.Add(new UIButton("RESTART", new Vector2(549.5f, 410), _contentManager.Load<Texture2D>("Sprites/UI/PauseMenu/RESTART"), true));
            _buttons.Add(new UIButton("MAINMENU", new Vector2(549.5f, 470), _contentManager.Load<Texture2D>("Sprites/UI/PauseMenu/MAINMENU"), true));
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState)
        {
            
            if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Released)
            {
                //When clicked
                int counter = 0;
                foreach (UIButton btn in _buttons)
                {
                    if (checkCollision(new Vector2(mouseState.X, mouseState.Y), btn))
                    {
                        Console.WriteLine("Clicked");
                        _previousMouseState = mouseState;
                        switch (btn.Name)
                        {
                            case "CONTINUE":
                                //CONTINUE METHOD
                                Console.WriteLine("CONTINUE");//HARDCODED FOR 
                                //_gameScreen.Reset("test2");//NEED LOAD NEW LEVEL

                                if (_ui.PlayLevel.Equals("Level-1"))
                                {
                                    _ui.ChangePanel(UIController.PanelType.MainMenu, _gameScreen);
                                }

                                else if (_ui.PlayLevel.Equals("Tutorial-7"))
                                {
                                    _ui.ChangePanel(UIController.PanelType.MainMenu, _gameScreen);
                                }
                                else
                                {
                                    _gameScreen.Reset("Tutorial-" + (int.Parse(_ui.PlayLevel[_ui.PlayLevel.Length - 1] + "") + 1));
                                }

                                break;

                            case "RESTART":
                                //PAUSE METHOD
                                Console.WriteLine("RESTART");
                                _gameScreen.Reset();
                                break;

                            case "MAINMENU":
                                //PAUSE METHOD
                                Console.WriteLine("MAINMENU");
                                _ui.ChangePanel(UIController.PanelType.MainMenu, _gameScreen);
                                break;

                            default:
                                break;
                        }
                        break;
                    }
                }
            }
            else if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
                Console.WriteLine("Released");
                _previousMouseState = mouseState;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
               //DRAG
            }
            
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(_texture);
            spriteBatch.Begin();
            spriteBatch.Draw(_pausebg,new Vector2(0,0), Color.White);
            
            int multiplier = (int)(60/_gameScreen.TotalTime);
            if (multiplier == 0)
                multiplier = 1;
            if(_ui.PlayLevel.Equals("Level-1"))
                spriteBatch.DrawString(_fontTimer, "Score: " + (_gameScreen.Money + 80000), new Vector2(519.5f, 245), Color.White);
            else
               spriteBatch.DrawString(_fontTimer, "Score: " + (_gameScreen.Money + (int.Parse(_ui.PlayLevel[_ui.PlayLevel.Length-1]+"") * 1000)*multiplier), new Vector2(519.5f, 245), Color.White);
            foreach (UIButton btn in _buttons)
            {
                spriteBatch.Draw(btn.Texture, btn.Position, Color.White);
            }
            spriteBatch.End();
        }

        private bool checkCollision(Vector2 mousePos, UIButton btn)
        {
            if (mousePos.X >= btn.Position.X // To the right of the left side
            && mousePos.X < btn.Position.X + btn.Texture.Width //To the left of the right side
            && mousePos.Y > btn.Position.Y //Below the top side
            && mousePos.Y < btn.Position.Y + btn.Texture.Height) //Above the bottom side
                return true; //We are; return true.
            return false; //We're not; return false.
        }
    }
}
